package com.epin.base.plugins;


import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.epin.base.defaults.AppConstants;
import com.epin.base.enums.commons.WebResultEnum;
import com.epin.business.system.entity.bo.UserNormal;
import com.epin.util.SessionUtil;


public abstract class BaseController {

	Logger logger = LoggerFactory.getLogger(BaseController.class);

	/**
	 * 超级管理员角色KEY值
	 */
	protected static final String ADMIN_ROLE_KEY = AppConstants.getAdminRoleKey();

	/**
	 * map默认大小
	 */
	protected static final int MAP_DEFAULT_SIZE = AppConstants.getMapDefaultSize();



	/**
	 * 成功,返回状态
	 * @return
	 */
	public AbstractBean success() {
		return new AbstractBean();
	}

	/**
	 * 成功,返回状态
	 * @return
	 */
	public AbstractBean success(Object data) {
		AbstractBean bean = new AbstractBean();
		bean.setData(data);
		return bean;
	}

	/**
	 * 失败,返回状态及原因
	 * @param result 消息
	 * @return
	 */
	public AbstractBean fail(WebResultEnum result) {
		AbstractBean bean = new AbstractBean();
		bean.setMessage(result.getName());
		bean.setStatus(result.getVal());
		return bean;
	}

	public AbstractBean fail(String message,String status) {
		AbstractBean bean = new AbstractBean();
		bean.setMessage(message);
		bean.setStatus(status);
		return bean;
	}

	public AbstractBean fail(String message){
		AbstractBean bean = new AbstractBean();
		bean.setMessage(message);
		bean.setStatus(WebResultEnum.ERROR.getVal());
		return bean;
	}

	public AbstractBean error() {
		AbstractBean bean = new AbstractBean();
		bean.setMessage(WebResultEnum.ERROR.getName());
		bean.setStatus(WebResultEnum.ERROR.getVal());
		return bean;
	}

	
	/**
	 * 
	 * @return
	 */
	public Map<String, Object> getParams() {
		
		UserNormal currentUser = SessionUtil.getCurrentUser();
		
		Map<String, Object> params = new HashMap<>();
		if(!ADMIN_ROLE_KEY.equals(currentUser.getRole().getKey())) {
			params.put("companyId", currentUser.getCompanyId());
			params.put("companyCode", currentUser.getCompany().getCompanyCode());
		}
		
		return params;
	}
	
	
	/**
	 * 
	 * @return
	 */
	public UserNormal getOperator(){
		return SessionUtil.getCurrentUser();
	}
	
	
	/**
	 * 
	 * @param flag
	 * @return
	 */
	public AbstractBean result(boolean flag){
		return flag ? success() : error();
	}

}

